REFACTOR: Make Translation/Variation/Persuasion inherit from LLMGenericTextConverter#1714
Open
romanlutz wants to merge 1 commit into
Open
REFACTOR: Make Translation/Variation/Persuasion inherit from LLMGenericTextConverter#1714romanlutz wants to merge 1 commit into
romanlutz wants to merge 1 commit into
Conversation
…icTextConverter
Adds three small extensibility points to LLMGenericTextConverter so the three
remaining LLM-backed text converters (Translation, Variation, Persuasion) can
inherit from it instead of duplicating the build-conversation/send/return loop:
- `_process_response` hook for response post-processing (.strip, JSON parse)
- `RETRY_EXCEPTIONS` class attribute (with per-instance `retry_exceptions`
override) opting subclasses into a unified retry helper that uses the same
env-controlled `RETRY_MAX_NUM_ATTEMPTS` knob as `pyrit_json_retry`,
plus `wait_exponential`, `reraise=True` and `log_exception`.
- `**kwargs` forwarded to `user_prompt_template_with_objective` rendering
so subclasses can pass static template parameters (e.g., language).
Other changes:
- Base `MessagePiece` now preserves the raw input as `original_value` and
the templated text as `converted_value` (previously the raw was lost when
a user template was set).
- Input-type validation moved before `set_system_prompt` (matches the
behaviour Variation/Persuasion already had).
- Translation's f-string user prompt is now a YAML SeedPrompt that renders
byte-for-byte equivalent text (regression test included).
- Translation's `max_retries` and `max_wait_time_in_seconds` ctor args are
deprecated for one release; retry attempts are env-controlled now.
- Three send-method shims (`_send_translation_prompt_async`,
`send_variation_prompt_async`, `send_persuasion_prompt_async`) emit
`DeprecationWarning` but still delegate to the unified helper.
- Persisted converter identifiers are unchanged for the three subclasses.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Refactors
TranslationConverter,VariationConverter, andPersuasionConverterto inherit fromLLMGenericTextConverterinstead of duplicating the build-conversation/send/return loop. Adds three small extensibility points to the base class so the three subclasses can be ~25-line thin wrappers.Why
Three converters (~500 lines combined) each reimplemented:
max_retries/max_wait_time_in_seconds; Variation/Persuasion:@pyrit_json_retry).strip(); Variation: parse JSON list, take[0]; Persuasion: parse JSON, extractmutated_text)The base class gains a
_process_responsehook and a unified retry helper that can produce both retry policies, so the duplication collapses cleanly.Base-class additions (
LLMGenericTextConverter)_process_response(response_text) -> strhook (default: identity)RETRY_EXCEPTIONS: tuple[type[BaseException], ...]class attribute (default()= no retry) + per-instanceretry_exceptionsconstructor overridemax_retry_attempts: int | Noneandretry_wait_max_seconds: int | Noneconstructor params for fixed-attempt + exponential-backoff policy; defaults give the same env-controlled, no-wait behavior aspyrit_json_retry**kwargsnow also forwarded touser_prompt_template_with_objectiverendering (in addition to system prompt)MessagePiecenow built withoriginal_value=raw_input, converted_value=templated_text(previously the raw input was lost when a user template was set)set_system_promptSubclass changes
TranslationConverter: inherits; passes bothmax_retry_attempts(=max_retries, default 3) andretry_wait_max_seconds(=max_wait_time_in_seconds, default 60) → behavior identical to before. F-string user prompt extracted intotranslation_user_prompt.yaml(byte-for-byte equivalent, regression-tested).VariationConverter: inherits;RETRY_EXCEPTIONS = (InvalidJsonException,); newvariation_user_prompt.yamlfor the per-call wrapper.PersuasionConverter: inherits;RETRY_EXCEPTIONS = (InvalidJsonException,); no user template (sends raw user prompt as before).Compatibility
Deprecations (one release):
VariationConverter.send_variation_prompt_async→ public shim emittingDeprecationWarning; delegates to_send_with_retries_asyncPersuasionConverter.send_persuasion_prompt_async→ same treatmentObservable bug-fix-style changes:
VariationConverter:MessagePiece.original_valuenow stores the raw user input instead of the templated=== begin === ... === end ===wrapper. Matches Translation/Persuasion convention.TranslationConverter:set_system_promptis no longer called on the target when input type is unsupported (validation moved before side effects).Preserved:
max_retries=3, max_wait_time_in_seconds=60defaults — fully functional, not deprecatedpyrit_json_retryexactly (same exception type, same env-controlled attempt count via_DynamicStopAfterAttempt/RETRY_MAX_NUM_ATTEMPTS, samereraise=True, no wait between attempts)LLMGenericTextConverterchildren (Tone, Tense, Noise, Denylist, Math, MaliciousQuestion, ScientificTranslation, ToxicSentenceGenerator) andRandomTranslationConverter's multiple inheritanceTests